package com.abe.libcore.utils

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.os.Bundle
import android.text.TextUtils
import com.abe.libcore.ConstantCore.NetWorkState

class NetWorkReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {
        if (TextUtils.isEmpty(intent!!.action)) return
        CommonUtils.log("NetWorkReceiver---Action:" + intent.action)
        when (intent.action) {
            ConnectivityManager.CONNECTIVITY_ACTION -> {
                val networkInfo = intent.getParcelableExtra<NetworkInfo>(ConnectivityManager.EXTRA_NETWORK_INFO)
                        ?: return
                val isConnect: Boolean
                //如果当前的网络连接成功并且网络连接可用
                isConnect = NetworkInfo.State.CONNECTED == networkInfo.state && networkInfo.isAvailable
                //广播发出去
                val bundle = Bundle()
                bundle.putBoolean("state", isConnect)
                val appIntent = Intent(NetWorkState)
                appIntent.putExtras(bundle)
                context!!.sendBroadcast(appIntent)
            }
        }
    }
}